package com.lkd.controller;

import com.lkd.dto.NodeDto;
import com.lkd.entity.Node;
import com.lkd.service.INodeService;
import com.lkd.vo.NodeVo;
import com.lkd.vo.PageBean;
import com.lkd.vo.RegionDetailVo;
import com.lkd.vo.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: NodeController
 * Package: com.lkd.controller
 * Description:
 *
 * @Author zt
 * @Create 2025/10/11 22:40
 * @Version 1.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/vm-service/node")
@Slf4j
public class NodeController {

    private final INodeService nodeService;

    /**
     * 查看区域详情
     * @param nodeDto
     * @return
     */
    @GetMapping("/search")
    public Result search(NodeDto nodeDto){
        PageBean<RegionDetailVo> pageBean = nodeService.search(nodeDto);
        return Result.success(pageBean);
    }

    /**
     *新增点位
     */
    @PostMapping
    public Result saveNode(@RequestBody Node node){
        //填充创建、更新时间字段
        node.setCreateTime(LocalDateTime.now());
        node.setUpdateTime(LocalDateTime.now());
        boolean result = nodeService.save(node);
        return Result.success(result);
    }

    /**
     * 获取点位详情
     */
    @GetMapping("/vmList/{nodeId}")
    public Result queryNode(@PathVariable Integer nodeId){
        List<NodeVo> nodeVo = nodeService.queryByNodeId(nodeId);
        return Result.success(nodeVo);
    }

    /**
     * 修改点位
     */
    @PutMapping
    public Result updateNode(@RequestBody Node node){
        node.setUpdateTime(LocalDateTime.now());
        boolean update = nodeService.updateById(node);
        return Result.success(update);
    }

    /**
     * 删除点位
     */
    @DeleteMapping("/{nodeId}")
    public Result deleteNode(@PathVariable("nodeId") Integer id){
        boolean result = nodeService.removeById(id);
        return Result.success(result);
    }

    /**
     * 统计点位数量
     */
    @GetMapping("/count")
    public Result nodeCount(){
        Long count = nodeService.lambdaQuery().count();
        log.info("点位的数量：{}", count);
        return Result.success(count);
    }
}
